<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料号" prop="materialNo">
        <el-input
          v-model="queryParams.materialNo"
          placeholder="请输入物料号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产线" prop="line">
        <el-select
          v-model="queryParams.line"
          placeholder="产线"
          clearable
          style="width: 240px"
          @change="getStationData(queryParams.line)"
        >
          <el-option
            v-for="item in proline_data"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工位" prop="station">
        <el-select
          v-model="queryParams.station"
          placeholder="工位"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in station_data"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出库类型" prop="outType">
        <el-select
          v-model="queryParams.outType"
          placeholder="出库类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in type"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!-- 状态-->
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in status"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="出库时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:spare:history:out:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:spare:history:out:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleReturnUpdate"
        >备件退库申请
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :selectable="isBanStatus"/>
      <el-table-column label="物料号" align="center" prop="materialNo"/>
      <el-table-column label="产线" align="center" prop="line"/>
      <el-table-column label="工位" align="center" prop="station"/>
      <el-table-column label="出库类型" align="center" prop="outType"/>
      <el-table-column label="出库数量" align="center" prop="outNum"/>
      <el-table-column label="出库人" align="center" prop="createBy"/>
      <el-table-column label="出库时间" align="center" prop="createTime"/>
      <!-- <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormatter"/> -->
      <el-table-column label="状态" width="350">
        <template slot-scope="scope" >
          <el-steps :active="scope.row.status + 1" finish-status="success" v-if="scope.row.status != 0">
            <el-step title="出库" ></el-step>
            <el-step title="退库申请"></el-step> <!-- :description="'退库申请人\n'+scope.row.returnRequestBy"-->
            <el-step title="一级审批"></el-step>
            <el-step title="二级审批(已退库)"></el-step>
        </el-steps>
        <el-steps :active="scope.row.status + 1" finish-status="success" v-if="scope.row.status == 0">
            <el-step title="出库" ></el-step>
        </el-steps>
        </template>
        
      </el-table-column>
      <!--      <el-table-column label="备注" align="center" prop="remark"/>-->
      
      <!-- 一级审批 icon="el-icon-check"-->
      <el-table-column label="一级审批" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status===1"
            @click="handleApprove1(scope.row)"
          >一级审批
          </el-button>
        </template>
      </el-table-column>
      <!-- 二级审批 icon="el-icon-finished"-->
      <el-table-column label="二级审批" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status===2"
            @click="handleApprove2(scope.row)"
          >二级审批
          </el-button>
        </template>
      </el-table-column>
      
      <el-table-column label="删除" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:spare:history:out:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listWarehouse, delWarehouse, getStatus } from '@/api/wms/warehouse'
import { getOutType, getProductLine, getStation } from '@/api/wms/equipment'
import { part_return_approval1,part_return_approval2,part_return } from '@/api/wms/w_return'

import router from '@/router'

export default {
  name: 'outHistory',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      warehouseList: [],
      proline_data: [],
      station_data: [],
      type: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialNo: null,
        line: null,
        station: null,
        outType: null,
        status:null,
        outNum: null,
        dateRange: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getProductLine()
    this.getOutType()
    this.getStatus()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getProductLine() {
      getProductLine().then(response => {
        this.proline_data = response.rows
      })
    },
    getStationData(line) {
      getStation(line).then(
        response => {
          this.station_data = response.rows
        }
      )
    },
    getList() {
      this.loading = true
      listWarehouse(this.addDateRange(this.queryParams, this.queryParams.dateRange)).then(response => {
        this.warehouseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getOutType() {
      getOutType().then(response => {
        this.type = response.rows
      })
    },
    getStatus(){
      getStatus().then(response => {
        this.status = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialNo: null,
        line: null,
        station: null,
        outType: null,
        outNum: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delWarehouse(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 一级审批 */
    handleApprove1(row){
      const ids = row.id || this.ids
      this.$modal.confirm('是否进行一级审批？').then(function() {
        return part_return_approval1(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('一级审批成功')
      }).catch(() => {
      })
      
    },
    /** 二级审批 */
    handleApprove2(row){
      const ids = row.id || this.ids
      this.$modal.confirm('是否进行二级审批？').then(function() {
        return part_return_approval2(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('二级审批成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/wms/warehouse/export', {
        ...this.queryParams
      }, `warehouse_${new Date().getTime()}.xlsx`)
    },
    handleReturnUpdate(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否申请退库？').then(function() {
        return part_return(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('申请退库成功')
      }).catch(() => {
      })
    },
    statusFormatter(status) {
      if (status === 0) {
        return '出库'
      } else if (status === 1) {
        return '待一级审批'
      } else if (status === 2) {
        return '待二级审批'
      } else if (status === 3) {
        return '已退库'
      }
    },
    isBanStatus(row, column) {
      const status = row.status
      if (status === 0) {
        return true
      } else if (status === 3) {
        return false
      }
    }
  }
  }
</script>
